//
//  UpdateUserNameView.m
//  location
//
//  Created by Dongxiang Xu on 2025/10/10.
//

#import "UpdateUserNameView.h"

@interface UpdateUserNameView ()<UITextFieldDelegate>

@end

@implementation UpdateUserNameView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView
{
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, SCREEN_HEIGHT, SCREEN_WIDTH-32, 280)];
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:40];
    infoView.backgroundColor = RealWhiteColor;
    [self addSubview:infoView];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(infoView.width-46, 16, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [infoView addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 46, infoView.width, 34)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"修改用户名";
    title.textColor = BlackColor;
    title.font = MEDIUM_FONT(20);
    [infoView addSubview:title];
    
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(32, CGRectGetMaxY(title.frame)+24, infoView.width-64, 52)];
    inputView.layer.masksToBounds = YES;
    [inputView.layer setCornerRadius:16];
    inputView.backgroundColor = LightBGColor;
    [infoView addSubview:inputView];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(16, 0, inputView.width-32, inputView.height)];
    textField.font = DEFAULT_FONT(14);
    textField.textColor = RealBlackColor;
    textField.placeholder = @"输入姓名";
    textField.returnKeyType = UIReturnKeyDone;
    [textField addTarget:self action:@selector(textChangeEvent:) forControlEvents:UIControlEventEditingChanged];
    textField.delegate = self;
    textField.enablesReturnKeyAutomatically = YES;
    [inputView addSubview:textField];
    [textField becomeFirstResponder];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(32, inputView.maxY+26, infoView.width-64, 52)];
    button.layer.masksToBounds = YES;
    [button.layer setCornerRadius:26];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button.titleLabel setFont:DEFAULT_FONT(17)];
    button.backgroundColor = NormalMainColor;
    [button setTitleColor:RealWhiteColor forState:UIControlStateNormal];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (textField.text.length>0) {
            [UIView animateWithDuration:0.3 animations:^{
                infoView.y = SCREEN_HEIGHT;
                self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
            if (self.UpdateUserNameBlock) {
                self.UpdateUserNameBlock(textField.text);
            }
        }else{
            [[HCToast shareInstance] showToast:@"输入姓名"];
        }
    }];
    [infoView addSubview:button];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self insertSubview:bgView atIndex:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bgView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        infoView.y = 212;
        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
    }];
}

- (void)textChangeEvent:(UITextField *)textField
{
    if (textField.text.length>=10) {
        textField.text = [textField.text substringToIndex:10];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
